﻿using System.Collections.Generic;
using Script.Framework;
using Script.UI;

namespace Script.Character
{
    public class CharacterBase : MonoBase
    {
        /// <summary>
        /// 自身关心的消息集合
        /// </summary>
        private readonly List<int> list = new List<int>();


        /// <summary>
        /// 绑定一个或多个消息
        /// </summary>
        /// <param name="eventCodes"></param>
        protected void Bind(params int[] eventCodes)
        {
            list.AddRange(eventCodes);
            CharacterManager.sInstance.Add(list.ToArray(), this);
        }


        /// <summary>
        /// 解除绑定的消息
        /// </summary>
        private void UnBind()
        {
            UIManager.mInstance.Remove(list.ToArray(), this);
            list.Clear();
        }

        /// <summary>
        /// 自动解除绑定的消息
        /// </summary>
        public virtual void OnDestroy()
        {
            if (list != null)
            {
                UnBind();
            }
        }

        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="areaCode"></param>
        /// <param name="eventCode"></param>
        /// <param name="message"></param>
        protected void Dispatch(int areaCode, int eventCode, object message)
        {
            MsgCenter.instance.Dispatch(areaCode, eventCode, message);
        }
    }
}